package service

import (
	"app.com/rpc.v1/internal/dao"
	"app.com/rpc.v1/internal/model"
	demo "app.com/rpc.v1/rpc/proto"
	"context"
	"fmt"
	"google.golang.org/grpc/metadata"
	"strconv"
)

type DemoService struct {
	demo.UnimplementedDemoServer
	dao *dao.Dao
}

func NewDemoService(dao *dao.Dao) *DemoService {
	return &DemoService{
		dao: dao,
	}
}

func (s *DemoService) TestDemo(ctx context.Context, req *demo.TestDemoReq) (rsp *demo.TestDemoRsp, err error) {
	md, _ := metadata.FromIncomingContext(ctx)
	fmt.Printf("Test md %v \n", md)

	rsp = &demo.TestDemoRsp{
		Res: "请求的ID为：" + strconv.FormatInt(req.Id, 10),
		Aa:  req.Name,
	}
	user := &model.User{
		Name: req.Name,
	}
	err = s.dao.UpdateUser(ctx, 1, user)
	if err != nil {
		return nil, err
	}
	return
}
